package com.ysj.mall.controller;

import com.ysj.mall.common.R;
import com.ysj.mall.entity.YsjUser;
import com.ysj.mall.service.YsjUserService;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpSession;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Objects;

@Controller
@Slf4j
public class YsjLoginController {
    @Resource
    private YsjUserService ysjUserService;
    @PostMapping("/login")
    @ResponseBody
    public R login(@RequestBody YsjUser user, HttpSession session) {
        log.info("登录的用户名:{},密码:{}", user.getUsername(),user.getPassword());
        // 这里应该添加实际的验证逻辑
        YsjUser loginUser = ysjUserService.login(user);
        if (Objects.nonNull(loginUser)) {
            loginUser.setPassword(null);
            session.setAttribute("user", loginUser);
            return R.success();
        } else {
            session.setAttribute("errorMessage","账户密码错误");
            return R.error("账户密码错误");
        }
    }
}
